package cn.juforg.airled;

import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public abstract class AirLEDSurface extends SurfaceView implements SurfaceHolder.Callback{
	
	SurfaceHolder holder = getHolder();
	AirLEDThread thread;
	public AirLEDSurface(Context context, AttributeSet attrs) {
		super(context, attrs);
		this.holder.addCallback(this);
		this.thread = new AirLEDThread();
	}
	public AirLEDSurface(Context context) {
		super(context);
		this.holder.addCallback(this);
		this.thread = new AirLEDThread();
	}
	
	class AirLEDThread extends Thread{
		private boolean running = true;

		public AirLEDThread() {
		}
		
		public void requestExitAndWait(){
			this.running = false;
			try {
				join();
				return;
			} catch (InterruptedException e) {
				e.printStackTrace();
				Log.e("AirLED-Error", e.getMessage());
			}
		}
		
		public void run(){
			while (true) {
				if(!this.running)
					return;
				doDraw();
			}
		}

		private void doDraw() {
			// TODO Auto-generated method stub
			
		}
	}
	
	
	protected abstract void doDraw(Canvas paramCanvas);
	
	public void surfaceChanged(SurfaceHolder paramSurfaceHolder, int paramInt1, int paramInt2, int paramInt3){
		
	}
	
	public void surfaceCreated(SurfaceHolder paramSurfaceHolder){
		this.thread = new AirLEDThread();
		this.thread.start();
	}
	
	public void surfaceDestory(SurfaceHolder paramSurfaceHolder){
		this.thread.requestExitAndWait();
		this.thread = null;
	}
	
}
